package com.lhnstart.learn.bases.homework;

/**
 * 字符串反转
 *
 * @author lhn
 * @date 2023/10/29 21:48
 * @since 1.0.0
 **/
public class HomeWork1 {

    public static void main(String[] args) {
        String string = "123456";
        System.out.println(reverse(string, 1, 4));
    }

    /**
     * 思路:
     * 先把 string 转成 char[]
     * */
    public static String reverse(String str, int start, int end) {
        // 对输入的参数做一个验证
        // 重要的编程技巧分享: 1: 写出正确的情况 2: 然后取反即可
        if (!(str != null && start >= 0 && end >= 0 && end > start && end < str.length())) {
            throw new RuntimeException("参数异常!");
        }


        char[] chars = str.toCharArray();
        char temp;
        for (int i = start, j = end; i < j; i++, j--) {
            temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
        return new String(chars);
    }
}